<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class popup_model extends CI_Model {
	//모델 생성자 호출
	function popup_model(){
		parent::__construct();
		$this->load->helper('file');
	}
	
	/**
	* create contents file
	*
	* @access	public
	* @return	none
	*/
	function create($data){	
		$this->db->insert('ch_popup', $data);
		
	}
	
	/**
	 * 팝업 목록 검색조건
	 *
	 * @access	public
	 * @return	query where절 구문
	 */
	function popup_list_where($param){
		$keyname = $param['keyname'];
		$keyword = $param['keyword'];
		
		if($keyname && $keyword){
			$array_keyname = explode(',',$keyname);
			foreach($array_keyname as $key){
				$_where[] = "{$key} LIKE '%{$keyword}%'";
			}
			$where = implode(' OR ', $_where);
			return $this->db->where($where);
		}else{
			return null;
		}
	}
	
	function popup_list_total($param){
			
		$this->popup_list_where($param);
		$count = $this->db->count_all_results('ch_popup');
		return $count;
	}
	
	function popup_list($param){
		$page = $param['page'];
		$listnum = $this->config->item('ch_listnum');
		$offset = $listnum*($page-1);
		$this->popup_list_where($param);				
		$this->db->order_by('popup_id DESC');
		$query = $this->db->get('ch_popup',$listnum,$offset);
		//echo $this->db->last_query();
		return $query->result();
	}
	
	function dbjob($dbjob,$data=null){
		switch($dbjob){
			case('i'):
				$this->create($data);
			break;
			case('u'):
				$this->update($data);
			break;
			case('d'):
				$this->delete($data);
			break;
		}
	}
	
	function select($id){
		$this->db->select("*");
	  	$this->db->where('popup_id', $id);
	  	$query = $this->db->get('ch_popup');
	  	$row = $query->row_array();
	  	if($row){	  		
	  		$data['dbjob'] = 'u';
	  		$data['popup_id'] = $row['popup_id'];
	  		$data['title'] = $row['title'];
	  		$data['start_date'] = $row['start_date'];
	  		$data['end_date'] = $row['end_date'];
	  		$data['layer_yn'] = $row['layer_yn'];
	  		$data['open_yn'] = $row['open_yn'];
	  		$data['popup_style'] = $row['popup_style'];
	  		$data['popup_template'] = $row['popup_template'];
	  		$data['popup_image'] = $row['popup_image'];
	  		$data['popup_content'] = $row['popup_content'];
	  		$data['popup_top'] = $row['popup_top'];
	  		$data['popup_left'] = $row['popup_left'];
	  		$data['popup_height'] = $row['popup_height'];
	  		$data['popup_width'] = $row['popup_width'];
	  	}else{
	  		$data['dbjob'] = 'i';
	  		$data['popup_id'] = '';
	  		$data['title'] = '';
	  		$data['start_date'] = '';
	  		$data['end_date'] = '';
	  		$data['layer_yn'] = 'N';
	  		$data['open_yn'] = 'Y';
	  		$data['popup_style'] = 'I';
	  		$data['popup_template'] = '';
	  		$data['popup_image'] = '';
	  		$data['popup_content'] = '';
	  		$data['popup_top'] = '';
	  		$data['popup_left'] = '';
	  		$data['popup_height'] = '';
	  		$data['popup_width'] = '';
	  	}
	  	return $data;
	}
	
	function update($data){
		$this->db->where('popup_id',$this->input->post('id'));
		$this->db->update('ch_popup',$data);
	}
	
	function delete(){
		$id = $this->input->post('id');
		$this->db->where('popup_id', $id);
		$this->db->delete('ch_popup');
		
	}
	
}
?>